﻿using com.tdb.foundation;
using Posion.Model;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class CreateRoleView : BaseView
{
    [SerializeField] private Button _btn;

    [SerializeField] private GameObject _dialog;

    [SerializeField] private InputField _roleName;


    private CreateRoleContext _context;
    
    public override void OnEnter(BaseContext context, UnityAction callback)
    {
        _dialog.transform.localScale = Vector3.zero;
        gameObject.SetActive(true);
        _dialog.transform.DOScale(1, .25f);
        _btn.onClick.RemoveAllListeners();
        _btn.onClick.AddListener(OnConfirmBtnClick);
        _context = context as CreateRoleContext;
    }

    public override void OnExit(UnityAction callback)
    {
        _dialog.transform.DOScale(0, .25f).onComplete = callback.SafeInvoke;
    }

    private void OnConfirmBtnClick()
    {
        if (_roleName.text.IsNOTNullOrEmpty())
        {
            _context.SetPlayerName(_roleName.text);
            UIManager.Instance.OpenView(new LobbyContext(), OpenType.Single);
        }
        else
        {
            GlobalUIManager.Instance.ModalDialog.ShowTips(TxtConst.Instance.CreateRoomNotNull);
        }
    }
}


public class CreateRoleContext : BaseContext
{
    public void SetPlayerName(string playerName)
    {
        Singleton<PlayerModel>.Instance.RoleName = playerName;
    }

    public CreateRoleContext() : base(UIType.CreateRole)
    {
    }
}